﻿var grid = null;
var oldStartDate = "";
var oldEndDate = "";

$(function () {
    $("#txtStartDate,#txtEndDate").ligerDateEditor({
        showTime: false,
        format: "yyyy-MM-dd",
        width: 100,
        cancelable: false
    });

    $("#btnSearch").click(function () {
        search();
    });

    $('#btnSearch').click();
});

function bindCategory(queryData) {
    $("#category").ligerComboBox({
        width: 100,
        cancelable: false,
        url: "/Scheduling/SchedulingSummary/GetSchedulingCategoryByRecord?ram" + Math.random(),
        valueField: "CategoryId",
        textField: "CategoryCode",
        valueFieldID: 'hdnCategoryId',
        value: -1,
        parms: queryData
    });
}


//显示列表
function search(pageNumber) {
    var queryData = {
        personnelId: $("#txtPersonnelId").val(),
        personnelName: $("#txtPersonnelName").val(),
        categoryId: $("#hdnCategoryId").val(),
        startDate: $("#txtStartDate").val(),
        endDate: $("#txtEndDate").val()
    };

    if (oldStartDate != queryData.startDate || oldEndDate != queryData.endDate) {
        bindCategory(queryData);
        oldStartDate = queryData.startDate;
        oldEndDate = queryData.endDate;
    }

    var columns = getColumns(queryData);
    initTable(queryData, columns);
}

//加载列表
function initTable(queryData, columns) {
    grid = $("#maingrid").ligerGrid({
        height: '100%',
        url: '/Scheduling/SchedulingSummary/Search?ram=' + Math.random(),
        parms: queryData,
        pageSize: 30,
        heightDiff: 5,
        usePager: false,
        rownumbers: false,
        enabledSort: false,
        rowDraggable: false,
        enabledEdit: false,
        columns: columns
    });
    GridNoData($("#maingrid"));
}

//获取表头
function getColumns(queryData) {
    var columns =
        [
            { display: '工号', name: 'PersonnelId', width: 80, render: function (row) { return row.PersonnelId.indexOf('NEW') == 0 ? "" : row.PersonnelId } },
            { display: '姓名', name: 'PersonnelName', width: 80 }
        ];

    $.ajax({
        url: "/Scheduling/SchedulingSummary/GetSchedulingSumTitle",
        data: queryData,
        async: false,
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                for (var i = 0; i < d.Body.length; i++) {
                    var item = d.Body[i];
                    columns.push({
                        display: item.CategoryCode, name: item.CategoryId, width: 60, totalSummary:
                        {
                            render: function (suminf, column, cell) {
                                return suminf.sum;
                            }
                        }
                    });
                }
            }
            else if (d.Result == 0) {
                $.ligerDialog.error(d.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });

    return columns;
}
